package com.jie.server.modules.system.conreoller;

import com.jie.common.annotation.Log;
import com.jie.common.controller.BaseController;
import com.jie.common.enums.BusinessType;
import com.jie.common.page.TableDataInfo;
import com.jie.common.result.R;
import com.jie.server.modules.system.entity.Dict;
import com.jie.server.modules.system.service.IDictService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 字典前端控制层
 * @author: xiaojie
 * @since: 2024/12/16 0016
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/system/dict")
public class DictController extends BaseController {

    @Autowired
    private IDictService dictService;

    /**
     * 查询字典表列表
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dict dict) {
        startPage();
        List<Dict> list = dictService.selectDictList(dict);
        return getDataTable(list);
    }


    /**
     * 获取字典表详细信息
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public R getInfo(@PathVariable("dictId") Long dictId) {
        return R.ok(dictService.selectDictByDictId(dictId));
    }

    /**
     * 新增字典表
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:create')")
    @Log(title = "字典表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody Dict dict) {
        boolean success = dictService.create(dict);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改字典表
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:edit')")
    @Log(title = "字典表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{dictId}")
    public R update(@PathVariable Long dictId,  @RequestBody Dict dict) {
        dict.setDictId(dictId);
        boolean success = dictService.update(dict);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除字典表
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:remove')")
    @Log(title = "字典表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds) {
        return R.ok(dictService.deleteDictByDictIds(dictIds));
    }

    /**
     * 根据所有字典类型
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        List<Dict> list = dictService.selectDictTypeAll();
        return R.ok(list);
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@customSs.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        dictService.resetDictCache();
        return R.ok();
    }

}
